package com.muqi.app.user.widget;

import com.muqi.app.project.utils.ScreenUtils;
import com.muqi.app.qlearn.student.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.method.HideReturnsTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class RecordDetialWindow extends PopupWindow implements OnClickListener {
	private View conentView;
	private Context mContext;
	private LinearLayout button1;
	private LinearLayout button2;
	private LinearLayout button3;
	private LinearLayout button4;
	private LinearLayout button5;
	private LinearLayout ll_container;

	public interface RecordDetialWindowListener {
		void onClickRecordDetialWindowListener(int id);
	}

	private RecordDetialWindowListener clickClubWindowListener;

	@SuppressWarnings("deprecation")
	public RecordDetialWindow(final Activity context,
			RecordDetialWindowListener clickClubWindowListener) {
		this.clickClubWindowListener = clickClubWindowListener;
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.popup_recorddetial, null);
		// int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		this.setContentView(conentView);
		this.setWidth(2 * w / 3);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.AnimationPreview);
		button1 = (LinearLayout) conentView.findViewById(R.id.pop_button_01);
		button2 = (LinearLayout) conentView.findViewById(R.id.pop_button_02);
		button3 = (LinearLayout) conentView.findViewById(R.id.pop_button_03);
		button4 = (LinearLayout) conentView.findViewById(R.id.pop_button_04);
		button5 = (LinearLayout) conentView.findViewById(R.id.pop_button_05);
		ll_container = (LinearLayout) conentView
				.findViewById(R.id.pop_contaniner);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);
		button5.setOnClickListener(this);
	}

	/**
	 * showPopupWidow
	 * 
	 * @param parent
	 */
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			hindLastLine();
			this.showAsDropDown(parent, 0, 5);
		} else {
			this.dismiss();
		}
	}

	@Override
	public void onClick(View v) {
		if (clickClubWindowListener != null) {
			clickClubWindowListener
					.onClickRecordDetialWindowListener(v.getId());
		}
		RecordDetialWindow.this.dismiss();
	}

	public void isOwn(boolean f, boolean z) {
		if (f && !z) {
			button3.setVisibility(View.VISIBLE);
			button2.setVisibility(View.GONE);
			button5.setVisibility(View.VISIBLE);
		} else if(f && z){
			button3.setVisibility(View.GONE);
			button2.setVisibility(View.GONE);
			button5.setVisibility(View.VISIBLE);

		}else{
			button3.setVisibility(View.GONE);
			button2.setVisibility(View.VISIBLE);
			button5.setVisibility(View.GONE);
		}
	}

	public void isShowZhuanZai(boolean p, boolean f) {
		if (p && f) {
			button4.setVisibility(View.VISIBLE);
		} else {
			button4.setVisibility(View.GONE);
		}

	}

	public void hindLastLine() {
		int childSize = ll_container.getChildCount();
		int lastVisible = 4;
		for (int i = 0; i < childSize; i++) {

			if (ll_container.getChildAt(i).getVisibility() == View.VISIBLE) {
				lastVisible = i;
			}

		}

		if (lastVisible != 4) {
			((LinearLayout) ll_container.getChildAt(lastVisible)).getChildAt(1)
					.setVisibility(View.GONE);

		}
	}

}
